//
//  FileTools.m
//  FrameWork
//
//  Created by Eagle on 5/26/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "FileTools.h"

bool IsPointInRect(CGPoint p, CGRect rect)
{
	return p.x >= rect.origin.x && p.y >= rect.origin.y && p.x < rect.origin.x + rect.size.width && p.y < rect.origin.y + rect.size.height;
}

void readcsv(const char *filename, char fengei, int item_num, int line_index, char *data_recv, int item_length)
{
	char c_read;
	
	NSString *name = [[NSString alloc] initWithUTF8String: filename];
	NSBundle *tBundle = [NSBundle mainBundle];
	NSString * path = [tBundle pathForResource:name ofType:NULL];
	NSData *r_data = [[[NSData alloc] initWithContentsOfFile:path] autorelease];
	
	[name release];
	int m_range = 0;
	int m_line = 0;
	int m_item_index = 0;
	int m_item_c_index = 0;
	
	if(r_data != NULL)
	{
		while (m_range < [r_data length]){
			[r_data getBytes:(&c_read) range:NSMakeRange(m_range, sizeof(char))];
			m_range++;
			//printf("%c", c_read);
			
			if (c_read == EOF){
				break;
			}
			
			if (c_read == '\r' || c_read == '\n'){
				m_line++;
				continue;
			}
			
			if (m_line == line_index && m_item_index < item_num){
				if (c_read == fengei){
					//data_recv[m_item_index] = line_data[m_item_index];
					
					m_item_index++;
					m_item_c_index = 0;
				}else{
					data_recv[m_item_index*item_length + m_item_c_index] = c_read;
					m_item_c_index++;
				}
			}else if (m_line > line_index){
				break;
			}
		}
	}
}




CWriter::CWriter()
{
	w_data = [[NSMutableData alloc] init];
}

CWriter::~CWriter()
{
	if (w_data){
		[w_data release];
	}
}

void CWriter::Close(const char *file_path)
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	//文件路径或者直接用~表示当前应用的目录
	NSString *name = [[NSString alloc] initWithUTF8String: file_path];
	
	NSString *filePath = [documentsDirectory stringByAppendingPathComponent:name]; 
	
	
	[w_data writeToFile:filePath atomically:YES];
	
	
	[name release];
}

void CWriter::WriteInt(int data)
{
	[w_data appendBytes:&data length:sizeof(data)];
}

void CWriter::WriteFloat(float data)
{
	[w_data appendBytes:&data length:sizeof(data)];
}

void CWriter::WriteData(char* data, int size)
{
	[w_data appendBytes:data length:size];
}

void CWriter::WriteString(const char* str)
{
	NSString *_str = [[NSString alloc] initWithUTF8String:str];
	[w_data appendData:[_str dataUsingEncoding:NSUTF8StringEncoding]];
}

CReader::CReader()
{
	r_data = NULL;
	m_range = 0;
}

CReader::~CReader()
{
	if (r_data){
		r_data = nil;
		[r_data release];
	}
}

bool CReader::OpenResource(const char* file_path)
{
	NSString *name = [[NSString alloc] initWithUTF8String: file_path];
	NSBundle *tBundle = [NSBundle mainBundle];
	NSString * path = [tBundle pathForResource:name ofType:NULL];
	r_data = [[NSData alloc] initWithContentsOfFile:path];
	
	[name release];
	return r_data != NULL;
}


bool CReader::Open(const char* file_path)
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	//文件路径或者直接用~表示当前应用的目录
	NSString *name = [[NSString alloc] initWithUTF8String: file_path];
	
	NSString *filePath = [documentsDirectory stringByAppendingPathComponent:name]; 
	
	[name release];
	//NSString *name = [[NSString alloc] initWithUTF8String:file_path];
	r_data = [NSData dataWithContentsOfFile:filePath];
	return r_data != NULL;
}
void CReader::Close()
{
	if (r_data){
		r_data = nil;
		[r_data release];
	}
}
void CReader::ReadInt(int* buf)
{
	[r_data getBytes:buf range:NSMakeRange(m_range, sizeof(int))];
	m_range += sizeof(int);
}
void CReader::ReadFloat(float* buf)
{
	[r_data getBytes:buf range:NSMakeRange(m_range, sizeof(float))];
	m_range += sizeof(float);
}
void CReader::ReadString(char* str, int buf_length)
{
	//NSString* strRead;
	//strRead = [[NSString alloc] initWithData:[r_data subdataWithRange:NSMakeRange(0, [tmpStr length])] encoding:NSUTF8StringEncoding];
	//memcpy(str, [strRead UTF8String], [strRead length]);
}

void CReader::ReadData(char* data, int size)
{
	[r_data getBytes:data range:NSMakeRange(m_range, size)];
	m_range += size;
}


